/*
Hermes Core: General Purpose Mobile Agent Platform
Copyright (C) 2006 UNICAM - Università degli Studi di Camerino

Group leaders: Flavio Corradini and Emanuela Merelli

Developers: Ezio Bartocci, Diego Bonura, Rosario Culmone,
Davide Lorenzoli, Leonardo Mariani, Francesca Piersigilli, 
Lorenzo Scortichini, Leonardo Vito.

GNU Lesser Public License

This library is free software; you can redistribute it 
and/or modify it under the terms of the GNU Lesser General 
Public License as published by the Free Software Foundation, 
version 2.1 of the License.

This library is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public 
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

Emanuela Merelli
Dipartimento di Matematica ed Informatica
Università di Camerino
via Madonna delle Carceri, 62032 Camerino, Italy
E.Mail: emanuela.merelli@unicam.it
*/
package hermesV2.util;

/**
 * This is the class that rappresents the customized logger for the hermes
 * plattform
 */
public class HermesLogger {
    /**
     * If it is true the log is on otherwise off.
     */
   // private static boolean log = false;

    /**
     * The current log level.
     */
    private static int level;

    /**
     * The driver of the logger
     */
    public static HermesLoggerDriver driver;

    /**
     * The level of the message to log.
     */
    public static final int DEBUG = 5;

    /**
     * The level of the message to log.
     */
    public static final int INFO = 4;

    /**
     * The level of the message to log.
     */
    public static final int WARNING = 3;

    /**
     * The level of the message to log.
     */
    public static final int ERROR = 2;

    /**
     * The level of the message to log.
     */
    public static final int FATAL = 1;

    /**
     * The level of the message to log.
     */
    public static final int OFF = 0;

    /**
     * This method sets the log level.
     * 
     * @param level
     *            The level of the logger (0 = logger off).
     */
    public static void setLevel(int level) {
        HermesLogger.level = level;
    }

    /**
     * This method sets the log level.
     * 
     * @param level
     *            The level of the logger.
     */
    public static void setLevel(String level) {
        if (level.equals("DEBUG")) {
            HermesLogger.level = HermesLogger.DEBUG;
        } else if (level.equals("INFO")) {
            HermesLogger.level = HermesLogger.INFO;
        } else if (level.equals("WARNING")) {
            HermesLogger.level = HermesLogger.WARNING;
        } else if (level.equals("ERROR")) {
            HermesLogger.level = HermesLogger.ERROR;
        } else if (level.equals("FATAL")) {
            HermesLogger.level = HermesLogger.FATAL;
        }
    }

    /**
     * Sets the current logger
     * 
     * @param aDriver
     *            The concrete logger.
     */
    public static void setLogger(HermesLoggerDriver aDriver) {
        driver = aDriver;
    }

    /**
     * Logs a message into the handler. It uses the trace stack to know which
     * class plus method called the log.
     * 
     * @param level
     *            The level of the message to log.
     * @param message
     *            the message to log.
     */
    public static void log(int level, String message) {
        if (driver != null) {
        if (HermesLogger.level >= level) {
                driver.log(level, message);
            }
        }
    }

    /**
     * Logs a message into the handler. It uses the trace stack to know which
     * class plus method called the log.
     * 
     * @param level
     *            The level of the message to log.
     * @param message
     *            the message to log.
     * @param e
     *            The exception to log.
     */
    public static void log(int level, String message, Exception e) {
        if (driver != null) {
            if (HermesLogger.level >= level) {
                    driver.log(level, message,e);
                }
            }
    }

    /**
     * Reset the logger, the driver will be null.
     */
    public static void reset() {
        driver = null;
    }
}